/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package helpers;

import java.util.Arrays;

/**
 *
 * @author William
 */
public class Validator {

    public static boolean validadeCpf(String cpf) {
        final String[] notCpf = {"00000000000", "11111111111", "22222222222", "33333333333", "44444444444", "55555555555", "66666666666", "77777777777", "88888888888", "99999999999"};
        int aux = 0;
        int rev;
        if (cpf == null || cpf.isEmpty()) {
            return false;
        }
        cpf = cpf
                .replace(".", "")
                .replace("-", "")
                .replace(" ", "")
                .trim();

        if (!cpf.matches("^\\d{11}$")) {
            return false;
        }

        for (String notV : notCpf) {
            if (cpf.equals(notV)) {
                return false;
            }
        }
        for (int i = 0; i < 9; i++) {
            aux += Character.getNumericValue(cpf.charAt(i)) * (10 - i);
        }

        rev = 11 - (aux % 11);

        if (rev == 10 || rev == 11) {
            rev = 0;
        }

        if (rev != Character.getNumericValue(cpf.charAt(9))) {
            return false;
        }

        aux = 0;

        for (int i = 0; i < 10; i++) {
            aux += Character.getNumericValue(cpf.charAt(i)) * (11 - i);
        }

        rev = 11 - (aux % 11);

        if (rev == 10 || rev == 11) {
            rev = 0;
        }

        return rev == Character.getNumericValue(cpf.charAt(10));
    }

    public static boolean validateRg(String rg) {
        if(rg == null || rg.isEmpty()) {
            return false;
        }
        rg = rg
                .replace(".", "")
                .replace("-", "")
                .replace(" ", "")
                .trim();
        
        return rg.matches("^\\d{9}$");
    }

    public static boolean validatePostalCode(String cep) {
        if (cep == null || cep.isEmpty()) {
            return false;
        }
        cep = cep
                .replace(" ", "")
                .replace("-", "")
                .trim();
        return cep.matches("^\\d{8}$");
    }

    public static boolean validatePhone(String phone) {
        final String[] notDDD = {"23", "25", "26", "29", "36", "39", "52", "56", "57", "58", "59", "72", "76", "78"};

        if (phone == null || phone.isEmpty()) {
            return false;
        }

        phone = phone
                .replace("(", "")
                .replace(")", "")
                .replace("-", "")
                .replace(" ", "")
                .trim();

        if (phone.length() < 10 || phone.length() > 11) {
            return false;
        }
        //Check if phone's DDD is invalid
        if (Arrays.asList(notDDD).contains(phone.substring(0, 2))) {
            return false;
        }
        //CONSERTAR ISSO
        return (phone.matches(""
                + "^([1-9]{2}9?[6-9]\\d{7})$|"
                + "^([1-9]{2}[2-5]\\d{7})$"));
    }
}
